#!/usr/bin/env bash
# sql_for_column_types -- Generates a SQL query that enumerates types of given table's columns
# > sql_for_column_types TABLE [COLUMN...]
##
set -eu

[[ $# -gt 0 ]] || usage "$0" "Missing TABLE"
Table=$1; shift

sqlForColumnNamesAndTypes="
    SELECT attname                          AS columnName
         , format_type(atttypid, atttypmod) AS columnType
      FROM pg_attribute
     WHERE attrelid = '$Table'::regclass
       AND attnum > 0
       AND NOT attisdropped
     ORDER BY attnum
    "

if [[ $# -gt 0 ]]; then
    for Column; do
        echo "COPY (
            SELECT columnType
            FROM ($sqlForColumnNamesAndTypes) columnNamesAndTypes
            WHERE columnName = '$Column'
        ) TO STDOUT;"
    done
else
    echo "COPY (
        SELECT columnType
        FROM ($sqlForColumnNamesAndTypes) columnNamesAndTypes
    ) TO STDOUT;"
fi
# TODO escaping each output line may be desirable
